<?php

class Router
{
    public static $Router;

    public static function getInstance(){
        if(self::$Router){
            return self::$Router;
        }else{
            return self::$Router=new Router();
        }
    }

    public function index()
    {
        $srcontent = file_get_contents('source/cache.php');
        if ($srcontent) {
            $content = unserialize($srcontent);
            $config=$content['config'];
            $routers_table = $content['routers_table'];
            $index = $content['index'];
            $R2R = $content['orders'][$index];//从哪个发送到哪个

            //清空之前标志位
            if ($index != 0) {
                $j = $index - 1;
            } else {
                $j = count($routers_table);
            }

            $table = $routers_table[$content['orders'][$j][1]];

            $temp = array();
            foreach ($table as $row) {
                $row['F'] = 0;
                $temp[] = $row;
            }
            $routers_table[$content['orders'][$j][1]] = $temp;


            //处理本次的结果(Rs->Rt+Rx=Rd)
            $Rs = $routers_table[$R2R[0]];
            $Rx = $routers_table[$R2R[1]];

            $Rt = array();
            foreach ($Rs as $row) {
                $row['R'] = $R2R[0];
                $row['D'] += 1;
                $Rt[] = $row;
            }

            $Rd = $Rx;
            $Ns = $this->getNs($routers_table[$R2R[1]]);
            foreach ($Rt as $row) {
                if (in_array($row['N'], $Ns)) {
                    $in = array_search($row['N'], $Ns);
                    if (($row['R'] === $Rd[$in]['R'] && $row['D'] != $Rd[$in]['D']) || $row['D'] < $Rd[$in]['D']) {//更新路由表
                        $row['F'] = 1;
                        $Rd[$in] = $row;
                    }
                } else { //若项目中的目的网络不在路由表中，则把该项目加到路由表中
                    $row['F'] = 1;
                    $Rd[] = $row;
                }
            }

            $routers_table[$R2R[1]] = $Rd;

            $orders = $content['orders'];
            $index++;
            if ($index >= count($orders)) {
                $index = 0;
            }
            $content = array('routers_table' => $routers_table, 'orders' => $orders, 'index' => $index,'config'=>$config);
            file_put_contents('source/cache.php', serialize($content));

            $src = 'source/network' . substr($R2R[0], strlen($R2R[0]) - 1, 1) . substr($R2R[1], strlen($R2R[1]) - 1, 1) . '.jpg';
            $Rsi = substr($R2R[0], strlen($R2R[0]) - 1, 1);
            $Rdi = substr($R2R[1], strlen($R2R[1]) - 1, 1);
           /* $this->display('RIP');*/
            if(isset($_GET['user'])&&$_GET['user']=='1'){
                include_once("view/user_RIP.html");
            }else{
                include_once("view/RIP.html");
            }


        } else {//未初始化
            //进行初始化
           $this->init();
        }

    }

    public function init(){
        $config = include('config.php');

        $routers = $config['routers'];
        $networks = $config['networks'];

        //初始化路由表
        $routers_table = array();
        $orders = array();
        foreach ($routers as $router) {
            $table = array();
            foreach ($config[$router]['link_networks'] as $network) {
                $table[] = array('N' => $network, 'R' => '-', 'D' => 1, 'F' => '0');
            }
            $routers_table[$router] = $table;

            foreach ($config[$router]['link_routers'] as $R) {
                $orders[] = array($router, $R);
            }
        }

        $index = 0;
        $src = 'source/network00.jpg';
        //保存路由信息
        $content = array('routers_table' => $routers_table, 'orders' => $orders, 'index' => $index,'config'=>$config);
        file_put_contents('source/cache.php', serialize($content));

      /*  $this->display('RIP');*/
        include_once("view/RIP.html");
    }


    private function getNs($table)
    {
        $Ns = array();
        foreach ($table as $row) {
            $Ns[] = $row['N'];
        }
        return $Ns;
    }

    private function  isGET(){
        if($_SERVER['REQUEST_METHOD']=='GET'){
            return 1;
        }else{
            return 0;
        }
    }


    //***************用户自定义××××××××××××××××××
    public function input(){
        if($this->isGET()){
            $srcontent = file_get_contents('source/cache.php');
            if ($srcontent) {
                $content = unserialize($srcontent);
                $config=$content['config'];
                $routers=implode(',',$config['routers']);
                $networks=implode(',',$config['networks']);

                $array=$config;
                $arrayR=array();
                foreach($config['routers'] as $R){
                    if(isset($config[$R])){
                        $link_routers=implode(',',$array[$R]['link_routers']);
                        $link_networks=implode(',',$array[$R]['link_networks']);
                        $arrayR[$R]=array('link_routers'=>$link_routers,'link_networks'=>$link_networks);
                    }
                }

            }

           include_once("view/input.html");
        }else{
          //  print_r($_POST);
            $config=array();
            $config['routers']=explode(',',$_POST['routers']);
            $config['networks']=explode(',',$_POST['routers']);

            foreach ($_POST['route'] as $i=>$r) {
                if($r!==''){
                    $link_routers=explode(',',$_POST['routeList'][$i]);
                    $link_networks=explode(',',$_POST['networkList'][$i]);
                    $config[$r]['link_routers']=$link_routers;
                    $config[$r]['link_networks']=$link_networks;
                }
            }

            $routers = $config['routers'];
            $networks = $config['networks'];

            //初始化路由表
            $routers_table = array();
            $orders = array();
            foreach ($routers as $router){
                $table = array();
                foreach ($config[$router]['link_networks'] as $network) {
                    $table[] = array('N' => $network, 'R' => '-', 'D' => 1, 'F' => '0');
                }
                $routers_table[$router] = $table;

                foreach ($config[$router]['link_routers'] as $R) {
                    $orders[] = array($router, $R);
                }
            }

            $index = 0;
            $src = 'source/network00.jpg';
            $content = array('routers_table' => $routers_table, 'orders' => $orders, 'index' => $index,'config'=>$config);
            file_put_contents('source/cache.php', serialize($content));

            $_GET['user']=1;
            $this->index();
        }
    }

    public function use_clear(){
        $srcontent = file_get_contents('source/cache.php');
        $content = unserialize($srcontent);
        $config=$content['config'];

        $routers = $config['routers'];
        $networks = $config['networks'];

        //初始化路由表
        $routers_table = array();
        $orders = array();
        foreach ($routers as $router){
            $table = array();
            foreach ($config[$router]['link_networks'] as $network) {
                $table[] = array('N' => $network, 'R' => '-', 'D' => 1, 'F' => '0');
            }
            $routers_table[$router] = $table;

            foreach ($config[$router]['link_routers'] as $R) {
                $orders[] = array($router, $R);
            }
        }

        $index = 0;
        $src = 'source/network00.jpg';
        $content = array('routers_table' => $routers_table, 'orders' => $orders, 'index' => $index,'config'=>$config);
        file_put_contents('source/cache.php', serialize($content));

        $_GET['user']=1;
        $this->index();
    }

}

?>